package com.group.Controller;

import cn.hutool.json.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group.Enity.Records;
import com.group.Service.RecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

@Controller
@RequestMapping("records")
public class RecordsController {

    @Autowired
    private RecordsService recordsService;

    @ResponseBody
    @RequestMapping("queryRecordsAll")
    public JSONObject queryRecordsAll(Integer page,Integer limit,String numbers,String typeId){

        //System.out.println(numbers + "," + typeId);

        PageHelper.startPage(page,limit);
        List<Records> list = recordsService.getRecords(numbers,typeId);

        PageInfo pageInfo = new PageInfo<>(list);
        Long len = pageInfo.getTotal();
        List<Records> list_new = pageInfo.getList();

        List<HashMap<String,Object>> list1 = new ArrayList();
        Iterator iterator = list_new.iterator();
        while(iterator.hasNext())
        {
            Records records = (Records) iterator.next();
            HashMap<String,Object> hm = new HashMap();

            hm.put("numbers",records.getHouse().getNumbers() );
            hm.put("username",records.getOwner().getUsername());
            hm.put("typename",records.getType().getName() );
            hm.put("num",records.getNum());
            hm.put("num2",records.getNum2());
            hm.put("checkTime",records.getCheckTime() );
            hm.put("meter",records.getMeter());
            hm.put("id",records.getId() );
            list1.add(hm);
        }


        JSONObject json=new JSONObject();
        json.accumulate("count",len);
        json.accumulate("code",0);
        json.accumulate("msg","null");
        json.accumulate("data",list1);
        return json;
    }

    @ResponseBody
    @RequestMapping("add")
    public String add(@RequestBody JSONObject jsonObject){
        //System.out.println(jsonObject);
        int houseId = jsonObject.getInt("houseId");
        String meter = jsonObject.getStr("meter");
        String onTime = jsonObject.getStr("onTime");
        int typeId = jsonObject.getInt("typeId");
        String remakrs = jsonObject.getStr("remarks");
        int num2 = jsonObject.getInt("num2");

        Records records = new Records();
        records.setHouse_id(houseId);
        records.setMeter(meter);
        records.setOnTime(onTime);
        records.setUpTime(onTime);
        records.setCheckTime(onTime);
        records.setType_id(typeId);
        records.setRemarks(remakrs);
        records.setNum2(num2);

        recordsService.add(records);

        return "";
    }

    @ResponseBody
    @RequestMapping("deleteByIds")
    public String deleteByIds(String ids){
        //System.out.println(ids);
        String[] lst = ids.split(",");
        for (int index = 0;index < lst.length;index++)
        {
            int id = Integer.valueOf(lst[index]);
            //buildingService.delete(id);
            recordsService.delete(id);
        }

        JSONObject json=new JSONObject();
        json.accumulate("code",200);
        return json.toString();
    }


}
